package cn.itsource.pethome.basic.service.impl;

import cn.itsource.pethome.basic.mapper.BaseMapper;
import cn.itsource.pethome.basic.query.BaseQuery;
import cn.itsource.pethome.basic.service.IBaseService;
import cn.itsource.pethome.basic.util.PageList;
import cn.itsource.pethome.org.domain.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class BaserServiceImpl<T> implements IBaseService<T> {

    @Autowired
    private BaseMapper<T> mapper;

    /**
     * 添加
     * @param t
     */
    @Override
    public void add(T t) {
        mapper.add(t);
    }

    /**
     * 修改
     * @param o
     */
    @Override
    public void update(T t) {
        mapper.update(t);
    }

    /**
     * 单个删除
     * @param id  主键id
     */
    @Override
    public void delete(Long id) {
        mapper.delete(id);
    }

    /**
     * 
     * @param id
     * @return
     */
    @Override
    public T findOne(Long id) {
        return null;
    }

    /**
     * 查询全部加上分页，高级查询
     * @param query
     * @return
     */
    @Override
    public PageList<T> findAll(BaseQuery query) {
        //查询条数
        Long cont = mapper.findCont(query);
        if (cont.equals(0L)){
            return new PageList<T>();
        }
        List<T> all = mapper.findAll(query);
        return new PageList<T>(cont,all);
    }

    /**
     * 批量删除
     * @param list
     */
    @Override
    public void batchRemove(List<T> list) {
        mapper.batchRemove(list);
    }

    /**
     * 查询全部
     * @return
     */
    @Override
    public List<T> loadAll() {
        return mapper.loadAll();
    }

    @Override
    public PageList<T> loadEAll(BaseQuery query) {
        //查询条数
        Long total=mapper.findCont(query);
        if (total.equals(0L)){
            return new PageList<>();
        }
        List<T> result=mapper.loadEAll(query);
        return new PageList<T>(total,result);
    }

    @Override
    public List<T> findEAll() {
        return mapper.findEAll();
    }
}
